﻿using System;
using System.Collections.Generic;
using System.Linq;
using Business.Data;

namespace Data
{
    public class StoreRepository : IStoreRepository
    {
        private readonly MvcMusicStoreEntities database;

        public StoreRepository()
        {
            this.database = new MvcMusicStoreEntities();
        }

        public IList<string> ListGenres()
        {
            return this.database.Genres.Select(genre => genre.Name).ToList();
        }

        public Business.Models.Genre LoadGenre(string genreName)
        {
            var dbGenre = this.database.Genres.Include("Albums").Single(genre => genre.Name.Equals(genreName));
            var businessGenre = new Business.Models.Genre
                                    {
                                        Id = (int)dbGenre.GenreId,
                                        Name = dbGenre.Name,
                                        Albums = dbGenre.Albums.Select(album => new Business.Models.Album
                                                                                    {
                                                                                        Genre = new Business.Models.Genre { Id = (int)dbGenre.GenreId, Name = dbGenre.Name },
                                                                                        Id = (int)album.AlbumId,
                                                                                        Title = album.Title
                                                                                    }).ToList()
                                    };
            return businessGenre;
        }

        public Business.Models.Album LoadAlbum(int albumId)
        {
            var result = this.database.Albums.Single(album => album.AlbumId.Equals(albumId));
            return new Business.Models.Album
                       {
                           Genre = new Business.Models.Genre { Id = (int)result.GenreId, Name = result.Genre.Name },
                           Id = (int)result.AlbumId,
                           Title = result.Title
                       };
        }

        public IList<Business.Models.Album> LoadAlbums()
        {
            var result = this.database.Albums.Include("Genre").Include("Artist");
            return result.Select(album => new Business.Models.Album
                                              {
                                                  Genre = new Business.Models.Genre { Id = (int)album.GenreId, Name = album.Genre.Name },
                                                  Id = (int)album.AlbumId,
                                                  Title = album.Title,

                                              }).ToList();
        }
    }
}